package com.example.myfirstapp2;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity implements Callback {

	public final static String EXTRA_MESSAGE = "com.example.myfirstapp2.MESSAGE";
	public static String AUCTION_BUYER_PAGE_PATTERN = null;
	Handler messageHandler = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		RegexTest rt = new RegexTest();
		rt.Test();

		AssetManager am = getAssets();

		InputStream in;
		try {
			in = am.open("AUCTION_BUYER_PAGE_PATTERN");

			InputStreamReader bf = new InputStreamReader(in, "euc-kr");

			char[] target = new char[1000];

			StringBuilder sb = new StringBuilder();

			while (true) {
				int readCount = bf.read(target);

				if (readCount == -1) {
					break;
				}
 
				sb.append(target, 0, readCount);

			}

			bf.close();

			AUCTION_BUYER_PAGE_PATTERN = sb.toString();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		messageHandler = new Handler(this);

		//AuctionLoginButtonOnClick(null);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean handleMessage(Message arg0) {

		//Vector<BuyerOrder> orders = (Vector<BuyerOrder>) arg0.obj;
		//System.out.println(orders.size());
		
		Intent intent = new Intent(this, DisplayOrdersActivity.class);
		startActivity(intent);

		return true;
	}

	public void AuctionLoginButtonOnClick(View view) {
		
		EditText editText1 = (EditText) findViewById(R.id.edit_auction_id);
		MyHttp.auctionId = editText1.getText().toString();
		
		EditText editText2 = (EditText) findViewById(R.id.edit_auction_password);
		MyHttp.auctionPw = editText2.getText().toString();

		BackgroundTread_GetAuctionOrders bk = new BackgroundTread_GetAuctionOrders(messageHandler);
		bk.start();

	}
}
